package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NAsphaltSensorCorrectionBo;
import com.ruoyi.zhgd.domain.vo.NAsphaltSensorCorrectionVo;
import com.ruoyi.zhgd.domain.NAsphaltSensorCorrection;
import com.ruoyi.zhgd.mapper.NAsphaltSensorCorrectionMapper;
import com.ruoyi.zhgd.service.INAsphaltSensorCorrectionService;
import com.baomidou.dynamic.datasource.annotation.DS;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 拌合站传感器校正Service业务层处理
 *
 * @author zyb
 * @date 2023-09-06
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NAsphaltSensorCorrectionServiceImpl implements INAsphaltSensorCorrectionService {

    private final NAsphaltSensorCorrectionMapper baseMapper;

    /**
     * 查询拌合站传感器校正
     */
    @Override
    public NAsphaltSensorCorrectionVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询拌合站传感器校正列表
     */
    @Override
    public TableDataInfo<NAsphaltSensorCorrectionVo> queryPageList(NAsphaltSensorCorrectionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NAsphaltSensorCorrection> lqw = buildQueryWrapper(bo);
        Page<NAsphaltSensorCorrectionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询拌合站传感器校正列表
     */
    @Override
    public List<NAsphaltSensorCorrectionVo> queryList(NAsphaltSensorCorrectionBo bo) {
        LambdaQueryWrapper<NAsphaltSensorCorrection> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NAsphaltSensorCorrection> buildQueryWrapper(NAsphaltSensorCorrectionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NAsphaltSensorCorrection> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStationId()), NAsphaltSensorCorrection::getStationId, bo.getStationId());
        lqw.eq(bo.getTemperature() != null, NAsphaltSensorCorrection::getTemperature, bo.getTemperature());
        lqw.eq(bo.getAsphalt() != null, NAsphaltSensorCorrection::getAsphalt, bo.getAsphalt());
        lqw.eq(bo.getHot1Warehouse() != null, NAsphaltSensorCorrection::getHot1Warehouse, bo.getHot1Warehouse());
        lqw.eq(bo.getHot2Warehouse() != null, NAsphaltSensorCorrection::getHot2Warehouse, bo.getHot2Warehouse());
        lqw.eq(bo.getHot3Warehouse() != null, NAsphaltSensorCorrection::getHot3Warehouse, bo.getHot3Warehouse());
        lqw.eq(bo.getHot4Warehouse() != null, NAsphaltSensorCorrection::getHot4Warehouse, bo.getHot4Warehouse());
        lqw.eq(bo.getHot5Warehouse() != null, NAsphaltSensorCorrection::getHot5Warehouse, bo.getHot5Warehouse());
        lqw.eq(bo.getHot6Warehouse() != null, NAsphaltSensorCorrection::getHot6Warehouse, bo.getHot6Warehouse());
        lqw.eq(bo.getPowder1Warehouse() != null, NAsphaltSensorCorrection::getPowder1Warehouse, bo.getPowder1Warehouse());
        lqw.eq(bo.getPowder2Warehouse() != null, NAsphaltSensorCorrection::getPowder2Warehouse, bo.getPowder2Warehouse());
        lqw.eq(bo.getPowder3Warehouse() != null, NAsphaltSensorCorrection::getPowder3Warehouse, bo.getPowder3Warehouse());
        return lqw;
    }

    /**
     * 新增拌合站传感器校正
     */
    @Override
    public Boolean insertByBo(NAsphaltSensorCorrectionBo bo) {
        NAsphaltSensorCorrection add = BeanUtil.toBean(bo, NAsphaltSensorCorrection.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改拌合站传感器校正
     */
    @Override
    public Boolean updateByBo(NAsphaltSensorCorrectionBo bo) {
        NAsphaltSensorCorrection update = BeanUtil.toBean(bo, NAsphaltSensorCorrection.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NAsphaltSensorCorrection entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除拌合站传感器校正
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
